package com.athuahua.spzx.service.impl;

import com.alibaba.excel.EasyExcel;
import com.athuahua.spzx.entity.vo.CategoryVo;
import com.athuahua.spzx.exception.CodeException;
import com.athuahua.spzx.listener.ExcelListener;
import com.athuahua.spzx.vo.product.CategoryExcelVo;
import com.athuahua.spzx.utils.ResultCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.athuahua.spzx.entity.Category;
import com.athuahua.spzx.service.CategoryService;
import com.athuahua.spzx.mapper.CategoryMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huahua
 * @description 针对表【category(商品分类)】的数据库操作Service实现
 * @createDate 2024-07-26 08:35:04
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryVo> findCategoryList(Long parentId) {
        //查询当前分类及是否存在子分类
        List<CategoryVo> result = new ArrayList<>();
        List<Category> categories = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, parentId)
                .orderByDesc(Category::getOrderNum)
        );

        for (Category category : categories) {
            CategoryVo categoryVo = new CategoryVo();

            Long l = categoryMapper.selectCount(new LambdaQueryWrapper<Category>()
                    .eq(Category::getParentId, category.getId()));
            //存在下一层分类
            categoryVo.setHasChildren(l > 0);

            BeanUtils.copyProperties(category, categoryVo);
            result.add(categoryVo);
        }


        return result;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        try {
            //1 设置响应信息和其他信息
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("分类数据", StandardCharsets.UTF_8);
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName + ".xlsx");
            //2 查询所有分类数据
            List<Category> categories = categoryMapper.selectList(null);
            List<CategoryExcelVo> categoryVos = new ArrayList<>();
            for (Category category : categories) {
                CategoryExcelVo categoryVo = new CategoryExcelVo();
                BeanUtils.copyProperties(category, categoryVo);
                categoryVos.add(categoryVo);
            }

            //3 调用EasyExcel工具类，导出数据
            EasyExcel.write(
                    response.getOutputStream(),
                    CategoryExcelVo.class
            ).sheet("分类数据").doWrite(categoryVos);

        }catch (Exception e){
            log.error("导出分类数据失败", e);
            throw new CodeException(ResultCodeEnum.DATA_ERROR);
        }

    }

    @Override
    public void importData(MultipartFile file) {
        try {
            ExcelListener<CategoryExcelVo> excelListener = new ExcelListener<>(categoryMapper);
            EasyExcel.read(
                    file.getInputStream(),
                    CategoryExcelVo.class,
                    excelListener
            ).sheet().doRead();





        }catch (Exception e){
            log.error("导入分类数据失败", e);
            throw new CodeException(ResultCodeEnum.DATA_ERROR);
        }
    }
}




